﻿namespace Oddo.Concept.Crm.Common.Presentation.Extensions
{
    using System;
    using System.Windows;
    using System.Windows.Media.Animation;
    using System.Windows.Threading;

    public static class Extensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="duration">Durée de l'animation</param>
        /// <param name="callback">Action a appeller à la fin de l'animation</param>
        public static void FadeIn(this UIElement element, TimeSpan duration, Action callback)
        {
            var sb = new Storyboard();
            if (callback != null)
                sb.Completed += (s, e) => callback();
            var animation = new DoubleAnimation(0.0, 0.1, new Duration(duration));
            Storyboard.SetTarget(animation, element);
            Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
            sb.Children.Add(animation);
            element.Dispatcher.BeginInvoke(new Action(sb.Begin), DispatcherPriority.Render);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="element"></param>
        /// <param name="duration">Durée de l'animation</param>
        /// <param name="callback">Action a appeller à la fin de l'animation</param>
        public static void FadeOut(this UIElement element, TimeSpan duration, Action callback)
        {
            var sb = new Storyboard();
            if(callback!=null)
                sb.Completed += (s, e) => callback();
            var animation = new DoubleAnimation(1.0, 0.0, new Duration(duration));
            Storyboard.SetTarget(animation, element);
            Storyboard.SetTargetProperty(animation,new PropertyPath(UIElement.OpacityProperty));
            sb.Children.Add(animation);
            element.Dispatcher.BeginInvoke(new Action(sb.Begin), DispatcherPriority.Render);
        }

    }
}